<template>
  <div>
    <template>
      <div class="custom-dropdown">
        <!-- 下拉框头部 -->
        <div class="dropdown-header" @click="toggleDropdown">
          {{ selectedOption || placeholder }}
          <span class="arrow">{{ isOpen ? "▲" : "▼" }}</span>
        </div>

        <!-- 下拉框选项列表 -->
        <ul v-if="isOpen" class="dropdown-list">
          <!-- 使用插槽接收父组件传递的选项 -->
          <slot></slot>
        </ul>
      </div>
    </template>
  </div>
</template>
<script setup>
import { ref } from "vue";

// 定义组件的 props
const props = defineProps({
  placeholder: {
    type: String,
    default: "请选择",
  },
  selectedOption: {
    type: String,
    default: "",
  },
});

// 控制下拉框的展开与收起
const isOpen = ref(false);

// 切换下拉框状态
const toggleDropdown = () => {
  isOpen.value = !isOpen.value;
};
</script>
<style scoped>
.custom-dropdown {
  position: relative;
  display: inline-block;
  width: 200px;
}

.dropdown-header {
  padding: 10px;
  border: 1px solid #ccc;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dropdown-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  border: 1px solid #ccc;
  background-color: #fff;
  list-style: none;
  padding: 0;
  margin: 0;
  z-index: 1000;
}

.dropdown-list li {
  padding: 10px;
  cursor: pointer;
}

.dropdown-list li:hover {
  background-color: #f0f0f0;
}

.arrow {
  font-size: 12px;
}
</style>
